<template>
    <div class="login-container">
      <div class="login-box">
        <div class="logo">
          <img src="https://vuejs.org/images/logo.png" alt="Logo" />
        </div>
        <el-form :model="form" label-width="0">
          <el-form-item>
            <el-input v-model="form.username" placeholder="用户名" prefix-icon="User" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="Lock" show-password />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { useRouter } from 'vue-router';

const router = useRouter();
const form = ref({ username: 'admin', password: 'admin123' });

const login = async () => {
  try {
    const res = await axios.post('http://localhost:3000/api/login', form.value);
    localStorage.setItem('token', res.data.token);
    ElMessage.success('登录成功');
    router.push('/welcome'); // 修改跳转路径
  } catch (err) {
    ElMessage.error(err.response?.data?.message || '登录失败');
  }
};

const reset = () => {
  form.value = { username: '', password: '' };
};
</script>


  
  <style scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
  }
  .login-box {
    width: 300px;
    padding: 20px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  .logo {
    text-align: center;
    margin-bottom: 20px;
  }
  .logo img {
    width: 50px;
  }
  </style>